#include "general.h"
#if (SENSOR_TYPE == Sensor_PA6371)
#include "pv_api.h"
#include "sensor_cdsp.h"
#include "sp1k_aaa_api.h"

UINT8 sensorDriverGainClass = 0xff;

//UINT16 ctc_rgain[4]={228, 294, 329, 0};
//UINT16 ctc_bgain[4]={358, 343, 257, 0};
UINT16 ctc_rgain[4]={220, 332, 373, 420};
UINT16 ctc_bgain[4]={660, 576, 368, 360};
UINT8 RawR[256]=0;
UINT8 RawG[256]=0;
UINT8 RawB[256]=0;

#define MAX_FRAME_RATE_30_IDX 120	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_30_IDX 10	// New limited for OV3630 - Jim@0424
#define MAX_FRAME_RATE_15_IDX 120	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_15_IDX 10	// New limited for OV3630 - Jim@0424
code AEstr LVtbl60[121] =
	{
	{	50	,	333	,	79	,	0	}	,
	{	51	,	333	,	79	,	0	}	,
	{	52	,	333	,	79	,	0	}	,
	{	53	,	333	,	79	,	0	}	,
	{	54	,	333	,	79	,	0	}	,
	{	55	,	333	,	79	,	0	}	,
	{	56	,	333	,	79	,	0	}	,
	{	57	,	333	,	79	,	0	}	,
	{	58	,	333	,	78	,	0	}	,
	{	59	,	333	,	76	,	0	}	,
	{	60	,	333	,	74	,	0	}	,
	{	61	,	333	,	72	,	0	}	,
	{	62	,	333	,	70	,	0	}	,
	{	63	,	333	,	68	,	0	}	,
	{	64	,	333	,	67	,	0	}	,
	{	65	,	333	,	65	,	0	}	,
	{	66	,	333	,	64	,	0	}	,
	{	67	,	333	,	63	,	0	}	,
	{	68	,	333	,	60	,	0	}	,
	{	69	,	333	,	59	,	0	}	,
	{	70	,	333	,	57	,	0	}	,
	{	71	,	333	,	55	,	0	}	,
	{	72	,	333	,	53	,	0	}	,
	{	73	,	333	,	52	,	0	}	,
	{	74	,	333	,	50	,	0	}	,
	{	75	,	333	,	49	,	0	}	,
	{	76	,	333	,	48	,	0	}	,
	{	77	,	333	,	46	,	0	}	,
	{	78	,	333	,	43	,	0	}	,
	{	79	,	333	,	41	,	0	}	,
	{	80	,	333	,	39	,	0	}	,
	{	81	,	333	,	38	,	0	}	,
	{	82	,	333	,	36	,	0	}	,
	{	83	,	333	,	35	,	0	}	,
	{	84	,	333	,	33	,	0	}	,
	{	85	,	333	,	32	,	0	}	,
	{	86	,	333	,	31	,	0	}	,
	{	87	,	333	,	29	,	0	}	,
	{	88	,	333	,	27	,	0	}	,
	{	89	,	333	,	25	,	0	}	,
	{	90	,	333	,	23	,	0	}	,
	{	91	,	333	,	21	,	0	}	,
	{	92	,	333	,	20	,	0	}	,
	{	93	,	333	,	18	,	0	}	,
	{	94	,	333	,	17	,	0	}	,
	{	95	,	333	,	16	,	0	}	,
	{	96	,	333	,	15	,	0	}	,
	{	97	,	333	,	14	,	0	}	,
	{	98	,	500	,	19	,	0	}	,
	{	99	,	500	,	18	,	0	}	,
	{	100	,	500	,	17	,	0	}	,
	{	101	,	500	,	16	,	0	}	,
	{	102	,	500	,	15	,	0	}	,
	{	103	,	500	,	14	,	0	}	,
	{	104	,	1000	,	26	,	0	}	,
	{	105	,	1000	,	24	,	0	}	,
	{	106	,	1000	,	23	,	0	}	,
	{	107	,	1000	,	21	,	0	}	,
	{	108	,	1000	,	20	,	0	}	,
	{	109	,	1000	,	18	,	0	}	,
	{	110	,	1000	,	17	,	0	}	,
	{	111	,	1000	,	16	,	0	}	,
	{	112	,	1000	,	15	,	0	}	,
	{	113	,	1000	,	14	,	0	}	,
	{	114	,	1042	,	15	,	0	}	,
	{	115	,	1117	,	15	,	0	}	,
	{	116	,	1198	,	15	,	0	}	,
	{	117	,	1285	,	15	,	0	}	,
	{	118	,	1378	,	15	,	0	}	,
	{	119	,	1477	,	15	,	0	}	,
	{	120	,	1584	,	15	,	0	}	,
	{	121	,	1698	,	15	,	0	}	,
	{	122	,	1820	,	15	,	0	}	,
	{	123	,	1951	,	15	,	0	}	,
	{	124	,	2092	,	15	,	0	}	,
	{	125	,	2243	,	15	,	0	}	,
	{	126	,	2404	,	15	,	0	}	,
	{	127	,	2577	,	15	,	0	}	,
	{	128	,	2763	,	15	,	0	}	,
	{	129	,	2962	,	15	,	0	}	,
	{	130	,	3175	,	15	,	0	}	,
	{	131	,	3403	,	15	,	0	}	,
	{	132	,	3648	,	15	,	0	}	,
	{	133	,	3910	,	15	,	0	}	,
	{	134	,	4191	,	15	,	0	}	,
	{	135	,	4492	,	15	,	0	}	,
	{	136	,	4815	,	15	,	0	}	,
	{	137	,	5161	,	15	,	0	}	,
	{	138	,	5532	,	15	,	0	}	,
	{	139	,	5930	,	15	,	0	}	,
	{	140	,	6356	,	15	,	0	}	,
	{	141	,	6813	,	15	,	0	}	,
	{	142	,	7302	,	15	,	0	}	,
	{	143	,	7827	,	15	,	0	}	,
	{	144	,	8389	,	15	,	0	}	,
	{	145	,	8992	,	15	,	0	}	,
	{	146	,	9638	,	15	,	0	}	,
	{	147	,	10330	,	15	,	0	}	,
	{	148	,	11072	,	15	,	0	}	,
	{	149	,	11867	,	15	,	0	}	,
	{	150	,	12719	,	15	,	0	}	,
	{	151	,	13632	,	15	,	0	}	,
	{	152	,	14611	,	15	,	0	}	,
	{	153	,	15660	,	15	,	0	}	,
	{	154	,	16785	,	15	,	0	}	,
	{	155	,	17990	,	15	,	0	}	,
	{	156	,	19282	,	15	,	0	}	,
	{	157	,	20667	,	15	,	0	}	,
	{	158	,	22151	,	15	,	0	}	,
	{	159	,	23741	,	15	,	0	}	,
	/*{	160	,	25445	,	15	,	0	}	,
	{	161	,	27272	,	15	,	0	}	,
	{	162	,	29230	,	15	,	0	}	,
	{	163	,	31329	,	15	,	0	}	,
	{	164	,	33578	,	15	,	0	}	,
	{	165	,	35989	,	15	,	0	}	,
	{	166	,	38573	,	15	,	0	}	,
	{	167	,	41342	,	15	,	0	}	,
	{	168	,	44310	,	15	,	0	}	,
	{	169	,	47491	,	15	,	0	}	,
	{	170	,	50900	,	22	,	0	}	*/
	{	160	,	25445	,	15	,	0	}	,
	{	161	,	27272	,	17	,	0	}	,
	{	162	,	29230	,	16	,	0	}	,
	{	163	,	31329	,	19	,	0	}	,
	{	164	,	33578	,	18	,	0	}	,
	{	165	,	35989	,	17	,	0	}	,
	{	166	,	38573	,	21	,	0	}	,
	{	167	,	41342	,	20	,	0	}	,
	{	168	,	44310	,	18	,	0	}	,
	{	169	,	47491	,	17	,	0	}	,
	{	170	,	50900	,	22	,	0	}
	}	;

#define MAX_FRAME_RATE_25_IDX 120
#define MIN_FRAME_RATE_25_IDX 10
#define MAX_FRAME_RATE_12P5_IDX 120
#define MIN_FRAME_RATE_12P5_IDX 10

code AEstr LVtbl50[121] =
	{
	{	50	,	333	,	79	,	0	}	,
	{	51	,	333	,	79	,	0	}	,
	{	52	,	333	,	79	,	0	}	,
	{	53	,	333	,	79	,	0	}	,
	{	54	,	333	,	79	,	0	}	,
	{	55	,	333	,	79	,	0	}	,
	{	56	,	333	,	79	,	0	}	,
	{	57	,	333	,	79	,	0	}	,
	{	58	,	333	,	78	,	0	}	,
	{	59	,	333	,	76	,	0	}	,
	{	60	,	333	,	74	,	0	}	,
	{	61	,	333	,	72	,	0	}	,
	{	62	,	333	,	70	,	0	}	,
	{	63	,	333	,	68	,	0	}	,
	{	64	,	333	,	67	,	0	}	,
	{	65	,	333	,	65	,	0	}	,
	{	66	,	333	,	64	,	0	}	,
	{	67	,	333	,	63	,	0	}	,
	{	68	,	333	,	60	,	0	}	,
	{	69	,	333	,	59	,	0	}	,
	{	70	,	333	,	57	,	0	}	,
	{	71	,	333	,	55	,	0	}	,
	{	72	,	333	,	53	,	0	}	,
	{	73	,	333	,	52	,	0	}	,
	{	74	,	333	,	50	,	0	}	,
	{	75	,	333	,	49	,	0	}	,
	{	76	,	333	,	48	,	0	}	,
	{	77	,	333	,	46	,	0	}	,
	{	78	,	333	,	43	,	0	}	,
	{	79	,	333	,	41	,	0	}	,
	{	80	,	333	,	39	,	0	}	,
	{	81	,	333	,	38	,	0	}	,
	{	82	,	333	,	36	,	0	}	,
	{	83	,	333	,	35	,	0	}	,
	{	84	,	333	,	33	,	0	}	,
	{	85	,	333	,	32	,	0	}	,
	{	86	,	333	,	31	,	0	}	,
	{	87	,	333	,	29	,	0	}	,
	{	88	,	333	,	27	,	0	}	,
	{	89	,	333	,	25	,	0	}	,
	{	90	,	333	,	23	,	0	}	,
	{	91	,	333	,	21	,	0	}	,
	{	92	,	333	,	20	,	0	}	,
	{	93	,	333	,	18	,	0	}	,
	{	94	,	333	,	17	,	0	}	,
	{	95	,	333	,	16	,	0	}	,
	{	96	,	333	,	15	,	0	}	,
	{	97	,	333	,	14	,	0	}	,
	{	98	,	500	,	19	,	0	}	,
	{	99	,	500	,	18	,	0	}	,
	{	100	,	500	,	17	,	0	}	,
	{	101	,	500	,	16	,	0	}	,
	{	102	,	500	,	15	,	0	}	,
	{	103	,	500	,	14	,	0	}	,
	{	104	,	1000	,	26	,	0	}	,
	{	105	,	1000	,	24	,	0	}	,
	{	106	,	1000	,	23	,	0	}	,
	{	107	,	1000	,	21	,	0	}	,
	{	108	,	1000	,	20	,	0	}	,
	{	109	,	1000	,	18	,	0	}	,
	{	110	,	1000	,	17	,	0	}	,
	{	111	,	1000	,	16	,	0	}	,
	{	112	,	1000	,	15	,	0	}	,
	{	113	,	1000	,	14	,	0	}	,
	{	114	,	1042	,	15	,	0	}	,
	{	115	,	1117	,	15	,	0	}	,
	{	116	,	1198	,	15	,	0	}	,
	{	117	,	1285	,	15	,	0	}	,
	{	118	,	1378	,	15	,	0	}	,
	{	119	,	1477	,	15	,	0	}	,
	{	120	,	1584	,	15	,	0	}	,
	{	121	,	1698	,	15	,	0	}	,
	{	122	,	1820	,	15	,	0	}	,
	{	123	,	1951	,	15	,	0	}	,
	{	124	,	2092	,	15	,	0	}	,
	{	125	,	2243	,	15	,	0	}	,
	{	126	,	2404	,	15	,	0	}	,
	{	127	,	2577	,	15	,	0	}	,
	{	128	,	2763	,	15	,	0	}	,
	{	129	,	2962	,	15	,	0	}	,
	{	130	,	3175	,	15	,	0	}	,
	{	131	,	3403	,	15	,	0	}	,
	{	132	,	3648	,	15	,	0	}	,
	{	133	,	3910	,	15	,	0	}	,
	{	134	,	4191	,	15	,	0	}	,
	{	135	,	4492	,	15	,	0	}	,
	{	136	,	4815	,	15	,	0	}	,
	{	137	,	5161	,	15	,	0	}	,
	{	138	,	5532	,	15	,	0	}	,
	{	139	,	5930	,	15	,	0	}	,
	{	140	,	6356	,	15	,	0	}	,
	{	141	,	6813	,	15	,	0	}	,
	{	142	,	7302	,	15	,	0	}	,
	{	143	,	7827	,	15	,	0	}	,
	{	144	,	8389	,	15	,	0	}	,
	{	145	,	8992	,	15	,	0	}	,
	{	146	,	9638	,	15	,	0	}	,
	{	147	,	10330	,	15	,	0	}	,
	{	148	,	11072	,	15	,	0	}	,
	{	149	,	11867	,	15	,	0	}	,
	{	150	,	12719	,	15	,	0	}	,
	{	151	,	13632	,	15	,	0	}	,
	{	152	,	14611	,	15	,	0	}	,
	{	153	,	15660	,	15	,	0	}	,
	{	154	,	16785	,	15	,	0	}	,
	{	155	,	17990	,	15	,	0	}	,
	{	156	,	19282	,	15	,	0	}	,
	{	157	,	20667	,	15	,	0	}	,
	{	158	,	22151	,	15	,	0	}	,
	{	159	,	23741	,	15	,	0	}	,
	/*{	160	,	25445	,	15	,	0	}	,
	{	161	,	27272	,	15	,	0	}	,
	{	162	,	29230	,	15	,	0	}	,
	{	163	,	31329	,	15	,	0	}	,
	{	164	,	33578	,	15	,	0	}	,
	{	165	,	35989	,	15	,	0	}	,
	{	166	,	38573	,	15	,	0	}	,
	{	167	,	41342	,	15	,	0	}	,
	{	168	,	44310	,	15	,	0	}	,
	{	169	,	47491	,	15	,	0	}	,
	{	170	,	50900	,	22	,	0	}	*/
	{	160	,	25445	,	15	,	0	}	,
	{	161	,	27272	,	17	,	0	}	,
	{	162	,	29230	,	16	,	0	}	,
	{	163	,	31329	,	19	,	0	}	,
	{	164	,	33578	,	18	,	0	}	,
	{	165	,	35989	,	17	,	0	}	,
	{	166	,	38573	,	21	,	0	}	,
	{	167	,	41342	,	20	,	0	}	,
	{	168	,	44310	,	18	,	0	}	,
	{	169	,	47491	,	17	,	0	}	,
	{	170	,	50900	,	22	,	0	}
	};

#define MAX_GAIN_IDX (74)
#define MIN_GAIN_IDX  (14)

code UINT8 gaintbl[80] = {
		0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
		0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
		0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
		0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
		0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
	 };

#define K_GAIN_Large        50
#define K_GAIN_Middle		40
#define K_GAIN_Small		25

code UINT16 cmt[3][9] = {
	{0x0058,0x03D7,0x0011,0x03DF,0x007A,0x03E7,0x03EB,0x03DA,0x007B},
	{0x0045,0x03EF,0x000C,0x03E8,0x006B,0x03EC,0x03F0,0x03F4,0x005C},
	{0x0029,0x0010,0x0006,0x03F4,0x0058,0x03F4,0x03F8,0x0015,0x0033}};


#define MCLKMODE        0 /*mclk mode,'1'from I/O Pad,'0'from internal*/
#define PCLKMODE        1 /*pclk mode,'1'from I/O Pad,'0'from internal*/
#define PREV_MCLKDIV    4 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define PREV_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#define SNAP_MCLKDIV    4 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define SNAP_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#if (PCLKMODE == 1)
#define PREV_PCLKFREQ (UINT32) 12000000
#define SNAP_PCLKFREQ (UINT32) 12000000
#define CLK_RATIO     (UINT8) ((SNAP_PCLKFREQ/1000) * 128 / (PREV_PCLKFREQ/1000))
#else
#define PREV_PCLKFREQ (UINT32) 96000000 / PREV_MCLKDIV / PREV_PCLKDIV
#define SNAP_PCLKFREQ (UINT32) 96000000 / SNAP_MCLKDIV / SNAP_PCLKDIV
#define CLK_RATIO     (UINT8) ((UINT16) PREV_MCLKDIV * PREV_PCLKDIV * 128 / SNAP_MCLKDIV / SNAP_PCLKDIV)
#endif
#define PCLKphase 0
#define TGGPIOFUNC 0
#define TGGPIODIR 0x04
#define TGGPIOVAL 0

xdata UINT16 exptime;
extern xdata UINT32 G_SHUTTER;
extern xdata UINT8 G_GAIN;
extern xdata UINT8 PreGain;
extern xdata UINT32 PreShutter;
extern UINT8 HAL_CDSPeofWait( UINT8 Number);
extern void cdspColorMatrixRestore_Intr(void);
extern void cdspColorMatrixRestore(void);

void  HAL_PAI2C_Write(UINT8 RegAddr, UINT8 RegData)
{
	UINT8 reg_addr[1], reg_data[1];
	reg_addr[0] = RegAddr;
 	reg_data[0] = RegData;
 	HAL_WriteSSC(reg_addr, reg_data, 0x01, 0x00);  //Set Seq SSC Write
}

void  HAL_FrontInit (UINT8 CamMode) USING_0
{
	UINT8 tmp0;
	printf (" Initialize the Front Sensor_PA6371 : Enter !!,CamMode=%d \n",CamMode);

	HAL_SetFrontIOConfig(TGGPIOFUNC, 0x00, TGGPIODIR, TGGPIOVAL);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	tmp0 = XBYTE[0x20E4];
	tmp0 &= 0xF0;
	tmp0 |= PCLKphase;
	XBYTE[0x20E4] |= tmp0;
	HAL_SSCConfig(Sensor_Slave_Address, Sensor_SSC_Freq);
	XBYTE[0x2AB0] = 0x01;		      //Reset Sensor interface
	XBYTE[0x2AB0] = 0x00;
	HAL_SensorRegInit();
}

void  HAL_FrontPreviewSet(void) USING_0
{
	HAL_FrontVdWait(0,1);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	HAL_SetFrontImgCrop(Sensor_Preview_HOffset, Sensor_Preview_VOffset, Sensor_Preview_HSize, Sensor_Preview_VSize, 0x00);
	HAL_SetFrontInverse(Prev_Hsync_Inverse,Prev_Vsync_Inverse);
	HAL_SetFrontReshape(Sensor_Preview_Hfall, Sensor_Preview_Hrise, Sensor_Preview_HReshen, Sensor_Preview_Vfall, Sensor_Preview_Vrise, Sensor_Preview_VReshen);
	HAL_SetSensorPreview();
	XBYTE[0x2110]=Sensor_Preview_BayerPattern;
	// for pv edge (cdsp_task will update 0x21d0, 0x21d3, 0x21d9~0x21de to fixed pv data)
	XBYTE[0x21D3] = 0x04;
	XBYTE[0x21D5] = 0x80;
	XBYTE[0x21D8] = 0x73;
}
void  HAL_FrontSnapSet(UINT16 Hsize,UINT16 Vsize) USING_0
{
	HAL_SetFrontCLKSet(SNAP_MCLKDIV, SNAP_PCLKDIV, 0);
	HAL_SetFrontReshape(Sensor_Snap_Hfall, Sensor_Snap_Hrise, Sensor_Snap_HReshen, Sensor_Snap_Vfall, Sensor_Snap_Vrise, Sensor_Snap_VReshen);
	HAL_SetFrontImgCrop(Sensor_Snap_HOffset, Sensor_Snap_VOffset, Hsize, Vsize, 1);
	XBYTE[0x2110]=Sensor_Snap_BayerPattern;
	PreGain =0;
	PreShutter = 0;
	// for still edge (cdsp_task will update 0x21d0, 0x21d3, 0x21d9~0x21de to cdsppara.bin's data)
	XBYTE[0x21D5] = 0x18;
	XBYTE[0x21D8] = 0x63;
}
void  HAL_SensorRegInit(void)
{
	HAL_Wait(20);
	//bank 0
	HAL_PAI2C_Write(0xEF, 0x00);
	HAL_PAI2C_Write(0x03, 0x0b);
	HAL_PAI2C_Write(0x04, 0x0b);
	HAL_PAI2C_Write(0x05, 0x11);
	HAL_PAI2C_Write(0x06, 0x00);
	HAL_PAI2C_Write(0x07, 0x00);
	HAL_PAI2C_Write(0x08, 0x00);
	HAL_PAI2C_Write(0x09, 0x00);
	HAL_PAI2C_Write(0x0a, 0x00);
	HAL_PAI2C_Write(0x0b, 0x00);
	HAL_PAI2C_Write(0x0c, 0x00);
	HAL_PAI2C_Write(0x0d, 0x11);
	HAL_PAI2C_Write(0x0e, 0x01);
	HAL_PAI2C_Write(0x0f, 0x40);
	HAL_PAI2C_Write(0x10, 0x02);
	HAL_PAI2C_Write(0x11, 0xb0);
	HAL_PAI2C_Write(0x12, 0x01);
	HAL_PAI2C_Write(0x13, 0xfc);
	HAL_PAI2C_Write(0x14, 0x91);
	HAL_PAI2C_Write(0x15, 0x90);
	HAL_PAI2C_Write(0x16, 0x01);
	HAL_PAI2C_Write(0x17, 0x2c);
	HAL_PAI2C_Write(0x18, 0x01);
	HAL_PAI2C_Write(0x19, 0x40);
	HAL_PAI2C_Write(0x1a, 0x9c);
	HAL_PAI2C_Write(0x1b, 0x40);
	HAL_PAI2C_Write(0x1c, 0x95);
	HAL_PAI2C_Write(0x1d, 0x01);
	HAL_PAI2C_Write(0x1e, 0x62);
	HAL_PAI2C_Write(0x1f, 0x8e);
	HAL_PAI2C_Write(0x20, 0x84);
	HAL_PAI2C_Write(0x21, 0x66);
	HAL_PAI2C_Write(0x22, 0x58);
	HAL_PAI2C_Write(0x23, 0xa4);
	HAL_PAI2C_Write(0x24, 0x87);
	HAL_PAI2C_Write(0x25, 0x63);
	HAL_PAI2C_Write(0x26, 0x01);
	HAL_PAI2C_Write(0x27, 0x4c);
	HAL_PAI2C_Write(0x28, 0xf4);
	HAL_PAI2C_Write(0x29, 0x00);
	HAL_PAI2C_Write(0x2a, 0xf2);
	HAL_PAI2C_Write(0x2b, 0x66);
	HAL_PAI2C_Write(0x2c, 0xe8);
	HAL_PAI2C_Write(0x2d, 0x00);
	HAL_PAI2C_Write(0x2e, 0xdc);
	HAL_PAI2C_Write(0x2f, 0x64);
	HAL_PAI2C_Write(0x30, 0x00);
	HAL_PAI2C_Write(0x31, 0x00);
	HAL_PAI2C_Write(0x32, 0x00);
	HAL_PAI2C_Write(0x34, 0x00);
	HAL_PAI2C_Write(0x36, 0x03);
	HAL_PAI2C_Write(0x37, 0x3c);
	HAL_PAI2C_Write(0x38, 0xf5);
	HAL_PAI2C_Write(0x39, 0x10);
	HAL_PAI2C_Write(0x3a, 0x60);
	HAL_PAI2C_Write(0x3b, 0x0a);
	HAL_PAI2C_Write(0x3c, 0x14);
	HAL_PAI2C_Write(0x3d, 0x0a);
	HAL_PAI2C_Write(0x3e, 0x14);
	HAL_PAI2C_Write(0x3f, 0x0e);
	HAL_PAI2C_Write(0x40, 0x00);
	HAL_PAI2C_Write(0x41, 0x0b);
	HAL_PAI2C_Write(0x42, 0x13);
	HAL_PAI2C_Write(0x43, 0x02);
	HAL_PAI2C_Write(0x44, 0x10);
	HAL_PAI2C_Write(0x45, 0x00);
	HAL_PAI2C_Write(0x46, 0x01);
	HAL_PAI2C_Write(0x47, 0x14);
	HAL_PAI2C_Write(0x48, 0x03);
	HAL_PAI2C_Write(0x49, 0x1d);
	HAL_PAI2C_Write(0x4a, 0x1d);
	HAL_PAI2C_Write(0x4b, 0x10);
	HAL_PAI2C_Write(0x4c, 0x06);
	HAL_PAI2C_Write(0x4d, 0x07);
	HAL_PAI2C_Write(0x4e, 0x80);
	HAL_PAI2C_Write(0x4f, 0x80);
	HAL_PAI2C_Write(0x50, 0xa7);
	HAL_PAI2C_Write(0x51, 0xc2);
	HAL_PAI2C_Write(0x52, 0x1c);
	HAL_PAI2C_Write(0x53, 0xd4);
	HAL_PAI2C_Write(0x54, 0x0b);
	HAL_PAI2C_Write(0x55, 0x38);
	HAL_PAI2C_Write(0x56, 0x2f);
	HAL_PAI2C_Write(0x57, 0x28);
	HAL_PAI2C_Write(0x58, 0x01);
	HAL_PAI2C_Write(0x59, 0x08);
	HAL_PAI2C_Write(0x5a, 0x14);
	HAL_PAI2C_Write(0x5b, 0x29);
	HAL_PAI2C_Write(0x5c, 0x3c);
	HAL_PAI2C_Write(0x5d, 0x4e);
	HAL_PAI2C_Write(0x5e, 0x70);
	HAL_PAI2C_Write(0x5f, 0x8f);
	HAL_PAI2C_Write(0x60, 0xad);
	HAL_PAI2C_Write(0x61, 0xc9);
	HAL_PAI2C_Write(0x62, 0x01);
	HAL_PAI2C_Write(0x63, 0x30);
	HAL_PAI2C_Write(0x64, 0x03);
	HAL_PAI2C_Write(0x65, 0x81);
	HAL_PAI2C_Write(0x66, 0x21);
	HAL_PAI2C_Write(0x67, 0xc6);
	HAL_PAI2C_Write(0x68, 0x2d);
	HAL_PAI2C_Write(0x69, 0x04);
	HAL_PAI2C_Write(0x6a, 0x10);
	HAL_PAI2C_Write(0x6b, 0x06);
	HAL_PAI2C_Write(0x6c, 0x12);
	HAL_PAI2C_Write(0x6d, 0x2a);
	HAL_PAI2C_Write(0x6e, 0xd2);
	HAL_PAI2C_Write(0x6f, 0x70);
	HAL_PAI2C_Write(0x70, 0x00);
	HAL_PAI2C_Write(0x71, 0x01);
	HAL_PAI2C_Write(0x72, 0x00);
	HAL_PAI2C_Write(0x73, 0x50);
	HAL_PAI2C_Write(0x74, 0xff);
	HAL_PAI2C_Write(0x75, 0x56);
	HAL_PAI2C_Write(0x76, 0x33);
	HAL_PAI2C_Write(0x77, 0x08);
	HAL_PAI2C_Write(0x78, 0x00);
	HAL_PAI2C_Write(0x79, 0x08);
	HAL_PAI2C_Write(0x7a, 0x00);
	HAL_PAI2C_Write(0x7b, 0x06);
	HAL_PAI2C_Write(0x7c, 0x00);
	HAL_PAI2C_Write(0x7d, 0x07);
	HAL_PAI2C_Write(0x7e, 0xa0);
	HAL_PAI2C_Write(0x7f, 0x2f);
	HAL_PAI2C_Write(0x80, 0xc3);
	HAL_PAI2C_Write(0x81, 0x03);
	HAL_PAI2C_Write(0x82, 0x1c);
	HAL_PAI2C_Write(0x83, 0xf0);
	HAL_PAI2C_Write(0x84, 0x20);
	HAL_PAI2C_Write(0x85, 0x00);
	HAL_PAI2C_Write(0x86, 0x10);
	HAL_PAI2C_Write(0x89, 0x00);
	HAL_PAI2C_Write(0x8b, 0x00);
	HAL_PAI2C_Write(0x8d, 0x00);
	HAL_PAI2C_Write(0x8e, 0x01);
	HAL_PAI2C_Write(0x8f, 0x40);
	HAL_PAI2C_Write(0x90, 0x40);
	HAL_PAI2C_Write(0x91, 0x10);
	HAL_PAI2C_Write(0x92, 0x80);
	HAL_PAI2C_Write(0x93, 0x00);
	HAL_PAI2C_Write(0x94, 0x00);
	HAL_PAI2C_Write(0x95, 0x02);
	HAL_PAI2C_Write(0x96, 0x00);
	HAL_PAI2C_Write(0x97, 0x30);
	HAL_PAI2C_Write(0x98, 0x01);
	HAL_PAI2C_Write(0x99, 0x23);
	HAL_PAI2C_Write(0x9a, 0x28);
	HAL_PAI2C_Write(0x9b, 0x22);
	HAL_PAI2C_Write(0x9c, 0x2e);
	HAL_PAI2C_Write(0x9d, 0x26);
	HAL_PAI2C_Write(0x9e, 0x2b);
	HAL_PAI2C_Write(0x9f, 0x28);
	HAL_PAI2C_Write(0xa0, 0x32);
	HAL_PAI2C_Write(0xa1, 0x26);
	HAL_PAI2C_Write(0xa2, 0x2b);
	HAL_PAI2C_Write(0xa3, 0x28);
	HAL_PAI2C_Write(0xa4, 0x32);
	HAL_PAI2C_Write(0xa5, 0x35);
	HAL_PAI2C_Write(0xa6, 0x3e);
	HAL_PAI2C_Write(0xa7, 0x38);
	HAL_PAI2C_Write(0xa8, 0x43);
	HAL_PAI2C_Write(0xa9, 0x4e);
	HAL_PAI2C_Write(0xaa, 0x01);
	HAL_PAI2C_Write(0xab, 0xfc);
	HAL_PAI2C_Write(0xac, 0x00);
	HAL_PAI2C_Write(0xb9, 0x02);
	HAL_PAI2C_Write(0xd7, 0x80);
	HAL_PAI2C_Write(0xd8, 0x02);
	HAL_PAI2C_Write(0xd9, 0xe0);
	HAL_PAI2C_Write(0xda, 0x01);
	HAL_PAI2C_Write(0xdb, 0x00);
	HAL_PAI2C_Write(0xdc, 0x00);
	HAL_PAI2C_Write(0xdd, 0xff);
	HAL_PAI2C_Write(0xde, 0x00);
	HAL_PAI2C_Write(0xdf, 0xaa);
	HAL_PAI2C_Write(0xe0, 0xaa);
	HAL_PAI2C_Write(0xe1, 0x00);
	HAL_PAI2C_Write(0xe2, 0x02);
	HAL_PAI2C_Write(0xe3, 0x11);
	HAL_PAI2C_Write(0xe4, 0x11);
	HAL_PAI2C_Write(0xe5, 0x00);
	HAL_PAI2C_Write(0xe6, 0x80);
	HAL_PAI2C_Write(0xed, 0x00);
	HAL_PAI2C_Write(0xed, 0x01);
	//bank 1
	HAL_PAI2C_Write(0xEF, 0x01);
	HAL_PAI2C_Write(0x00, 0x09);
	HAL_PAI2C_Write(0x01, 0x30);
	HAL_PAI2C_Write(0x02, 0x02);
	HAL_PAI2C_Write(0x03, 0x89);
	HAL_PAI2C_Write(0x04, 0xfc);
	HAL_PAI2C_Write(0x05, 0x01);
	HAL_PAI2C_Write(0x06, 0x0f);
	HAL_PAI2C_Write(0x07, 0x0f);
	HAL_PAI2C_Write(0x08, 0x0f);
	HAL_PAI2C_Write(0x09, 0x0f);
	HAL_PAI2C_Write(0x0c, 0x03);
	HAL_PAI2C_Write(0x0d, 0xc8);
	HAL_PAI2C_Write(0x0e, 0x01);
	HAL_PAI2C_Write(0x0f, 0x00);
	HAL_PAI2C_Write(0x10, 0x1f);
	HAL_PAI2C_Write(0x11, 0x00);
	HAL_PAI2C_Write(0x12, 0x04);
	HAL_PAI2C_Write(0x13, 0x10);
	HAL_PAI2C_Write(0x14, 0x15);
	HAL_PAI2C_Write(0x15, 0x10);
	HAL_PAI2C_Write(0x16, 0x15);
	HAL_PAI2C_Write(0x17, 0x08);
	HAL_PAI2C_Write(0x18, 0x00);
	HAL_PAI2C_Write(0x19, 0x08);
	HAL_PAI2C_Write(0x1a, 0x00);
	HAL_PAI2C_Write(0x1b, 0x87);
	HAL_PAI2C_Write(0x1c, 0x02);
	HAL_PAI2C_Write(0x1d, 0xe7);
	HAL_PAI2C_Write(0x1e, 0x01);
	HAL_PAI2C_Write(0x21, 0x03);
	HAL_PAI2C_Write(0x22, 0x00);
	HAL_PAI2C_Write(0x23, 0x00);
	HAL_PAI2C_Write(0x24, 0x01);
	HAL_PAI2C_Write(0x27, 0x03);
	HAL_PAI2C_Write(0x2b, 0x48);
	HAL_PAI2C_Write(0x2c, 0x86);
	HAL_PAI2C_Write(0x2d, 0x02);
	HAL_PAI2C_Write(0x2e, 0x46);
	HAL_PAI2C_Write(0x2f, 0x48);
	HAL_PAI2C_Write(0x30, 0x62);
	HAL_PAI2C_Write(0x31, 0x46);
	HAL_PAI2C_Write(0x32, 0x5a);
	HAL_PAI2C_Write(0x33, 0x1e);
	HAL_PAI2C_Write(0x34, 0x88);
	HAL_PAI2C_Write(0x35, 0x00);
	HAL_PAI2C_Write(0x36, 0x00);
	HAL_PAI2C_Write(0x37, 0x00);
	HAL_PAI2C_Write(0x38, 0x00);
	HAL_PAI2C_Write(0x39, 0x64);
	HAL_PAI2C_Write(0x3b, 0x00);
	HAL_PAI2C_Write(0x3c, 0x00);
	HAL_PAI2C_Write(0x3d, 0x00);
	HAL_PAI2C_Write(0x3e, 0x00);
	HAL_PAI2C_Write(0x40, 0x02);
	HAL_PAI2C_Write(0x41, 0x57);
	HAL_PAI2C_Write(0x42, 0x0c);
	HAL_PAI2C_Write(0x43, 0xf2);
	HAL_PAI2C_Write(0x44, 0x1f);
	HAL_PAI2C_Write(0x45, 0x04);
	HAL_PAI2C_Write(0x46, 0x04);
	HAL_PAI2C_Write(0x48, 0x00);
	HAL_PAI2C_Write(0x49, 0x04);
	HAL_PAI2C_Write(0x4a, 0x00);
	HAL_PAI2C_Write(0x4b, 0x80);
	HAL_PAI2C_Write(0x4c, 0x40);
	HAL_PAI2C_Write(0x4d, 0x00);
	HAL_PAI2C_Write(0x50, 0x00);
	HAL_PAI2C_Write(0x51, 0x40);
	HAL_PAI2C_Write(0x58, 0x00);
	HAL_PAI2C_Write(0x59, 0xc0);
	HAL_PAI2C_Write(0x5a, 0xff);
	HAL_PAI2C_Write(0x5b, 0x03);
	HAL_PAI2C_Write(0x5c, 0x19);
	HAL_PAI2C_Write(0x60, 0x01);
	HAL_PAI2C_Write(0x6d, 0xc8);
	HAL_PAI2C_Write(0x6e, 0xc8);
	HAL_PAI2C_Write(0x6f, 0xc8);
	HAL_PAI2C_Write(0x70, 0xc8);
	HAL_PAI2C_Write(0x71, 0x00);
	HAL_PAI2C_Write(0x73, 0x00);
	HAL_PAI2C_Write(0x76, 0x00);
	HAL_PAI2C_Write(0x77, 0x10);
	HAL_PAI2C_Write(0x78, 0x00);
	HAL_PAI2C_Write(0x79, 0x00);
	HAL_PAI2C_Write(0x7a, 0x01);
	HAL_PAI2C_Write(0x7b, 0x00);
	HAL_PAI2C_Write(0x7c, 0x13);
	HAL_PAI2C_Write(0x7e, 0xff);
	HAL_PAI2C_Write(0x7f, 0x00);
	HAL_PAI2C_Write(0x80, 0x00);
	HAL_PAI2C_Write(0x82, 0x00);
	HAL_PAI2C_Write(0x84, 0x00);
	HAL_PAI2C_Write(0x85, 0x00);
	HAL_PAI2C_Write(0x86, 0x00);
	HAL_PAI2C_Write(0x87, 0x00);
	HAL_PAI2C_Write(0x88, 0x00);
	HAL_PAI2C_Write(0x89, 0x00);
	HAL_PAI2C_Write(0x8a, 0x00);
	HAL_PAI2C_Write(0x8b, 0x00);
	HAL_PAI2C_Write(0x8c, 0x00);
	HAL_PAI2C_Write(0x8d, 0x00);
	HAL_PAI2C_Write(0x8e, 0x00);
	HAL_PAI2C_Write(0x8f, 0x00);
	HAL_PAI2C_Write(0x90, 0x00);
	HAL_PAI2C_Write(0x91, 0x00);
	HAL_PAI2C_Write(0x96, 0x00);
	HAL_PAI2C_Write(0x97, 0x00);
	HAL_PAI2C_Write(0x98, 0x80);
	HAL_PAI2C_Write(0x99, 0x37);
	HAL_PAI2C_Write(0x9a, 0x58);
	HAL_PAI2C_Write(0x9b, 0x04);
	HAL_PAI2C_Write(0x9c, 0x4e);
	HAL_PAI2C_Write(0x9d, 0x18);
	HAL_PAI2C_Write(0x9e, 0x00);
	HAL_PAI2C_Write(0x9f, 0x10);
	HAL_PAI2C_Write(0xa0, 0x02);
	HAL_PAI2C_Write(0xa1, 0x01);
	HAL_PAI2C_Write(0xa2, 0x05);
	HAL_PAI2C_Write(0xa3, 0x03);
	HAL_PAI2C_Write(0xa4, 0x03);
	HAL_PAI2C_Write(0xa5, 0x00);
	HAL_PAI2C_Write(0x11, 0x01);
	//bank 2
	HAL_PAI2C_Write(0xEF, 0x02);
	HAL_PAI2C_Write(0x00, 0x00);
	HAL_PAI2C_Write(0x01, 0x00);
	HAL_PAI2C_Write(0x02, 0x00);
	HAL_PAI2C_Write(0x03, 0x00);
	HAL_PAI2C_Write(0x04, 0x00);
	HAL_PAI2C_Write(0x05, 0x00);
	HAL_PAI2C_Write(0x06, 0x00);
	HAL_PAI2C_Write(0x07, 0x00);
	HAL_PAI2C_Write(0x08, 0x00);
	HAL_PAI2C_Write(0x09, 0x00);
	HAL_PAI2C_Write(0x0a, 0x00);
	HAL_PAI2C_Write(0x0e, 0x00);
	HAL_PAI2C_Write(0x0f, 0x00);
	HAL_PAI2C_Write(0x10, 0x00);
	HAL_PAI2C_Write(0x13, 0x00);
	HAL_PAI2C_Write(0x17, 0x01);
	HAL_PAI2C_Write(0x18, 0x24);
	HAL_PAI2C_Write(0x19, 0x3c);
	HAL_PAI2C_Write(0x1a, 0x51);
	HAL_PAI2C_Write(0x1b, 0x63);
	HAL_PAI2C_Write(0x1c, 0x74);
	HAL_PAI2C_Write(0x1d, 0x84);
	HAL_PAI2C_Write(0x1e, 0xa1);
	HAL_PAI2C_Write(0x1f, 0xbc);
	HAL_PAI2C_Write(0x20, 0xd6);
	HAL_PAI2C_Write(0x21, 0xed);
	HAL_PAI2C_Write(0x22, 0x0f);
	HAL_PAI2C_Write(0x23, 0x85);
	HAL_PAI2C_Write(0x24, 0xf0);
	HAL_PAI2C_Write(0x25, 0x96);
	HAL_PAI2C_Write(0x26, 0x20);
	HAL_PAI2C_Write(0x27, 0x14);
	HAL_PAI2C_Write(0x28, 0x0a);
	HAL_PAI2C_Write(0x29, 0x0f);
	HAL_PAI2C_Write(0x2a, 0xf8);
	HAL_PAI2C_Write(0x2b, 0x00);
	HAL_PAI2C_Write(0x2c, 0x30);
	HAL_PAI2C_Write(0x2d, 0x18);
	HAL_PAI2C_Write(0x2e, 0x22);
	HAL_PAI2C_Write(0x2f, 0x12);
	HAL_PAI2C_Write(0x30, 0x11);
	HAL_PAI2C_Write(0x33, 0x01);
	HAL_PAI2C_Write(0x34, 0xf0);
	HAL_PAI2C_Write(0x55, 0x00);
	HAL_PAI2C_Write(0x56, 0x01);
	HAL_PAI2C_Write(0x57, 0x01);
	HAL_PAI2C_Write(0x58, 0x04);
	HAL_PAI2C_Write(0x59, 0x01);
	HAL_PAI2C_Write(0x5a, 0x03);
	HAL_PAI2C_Write(0x5b, 0x2f);
	HAL_PAI2C_Write(0x5c, 0x06);
	HAL_PAI2C_Write(0x5d, 0x01);
	HAL_PAI2C_Write(0x5e, 0x0f);
	HAL_PAI2C_Write(0x5f, 0x40);
	HAL_PAI2C_Write(0x60, 0x50);
	HAL_PAI2C_Write(0x61, 0x00);
	HAL_PAI2C_Write(0x62, 0x03);
	HAL_PAI2C_Write(0x63, 0x0f);
	HAL_PAI2C_Write(0x64, 0x00);
	HAL_PAI2C_Write(0x65, 0x03);
	HAL_PAI2C_Write(0x66, 0x0f);
	HAL_PAI2C_Write(0x67, 0x00);
	HAL_PAI2C_Write(0x68, 0x03);
	HAL_PAI2C_Write(0x69, 0x0f);
	HAL_PAI2C_Write(0x9b, 0x02);
	HAL_PAI2C_Write(0x9c, 0x80);
	HAL_PAI2C_Write(0x9d, 0x01);
	HAL_PAI2C_Write(0x9e, 0xe0);
	HAL_PAI2C_Write(0x9f, 0x00);
	HAL_PAI2C_Write(0xa0, 0x00);
	HAL_PAI2C_Write(0xa1, 0x00);
	HAL_PAI2C_Write(0xa2, 0x00);
	HAL_PAI2C_Write(0xa3, 0x00);
	HAL_PAI2C_Write(0xa4, 0x02);
	HAL_PAI2C_Write(0xa5, 0x00);
	HAL_PAI2C_Write(0xa6, 0xf0);
	HAL_PAI2C_Write(0xa8, 0x01);
	HAL_PAI2C_Write(0xa9, 0x20);
	HAL_PAI2C_Write(0xb0, 0x00);
	HAL_PAI2C_Write(0xb1, 0x00);
	HAL_PAI2C_Write(0xb2, 0x00);
	HAL_PAI2C_Write(0xb3, 0x00);
	HAL_PAI2C_Write(0xb4, 0x02);
	HAL_PAI2C_Write(0xb5, 0x80);
	HAL_PAI2C_Write(0xb6, 0x01);
	HAL_PAI2C_Write(0xb7, 0xe0);
	HAL_PAI2C_Write(0xb8, 0x00);
	HAL_PAI2C_Write(0xb9, 0x00);
	HAL_PAI2C_Write(0xba, 0x00);
	HAL_PAI2C_Write(0xbb, 0x00);
	HAL_PAI2C_Write(0xc0, 0x80);
	HAL_PAI2C_Write(0xc1, 0x03);
	HAL_PAI2C_Write(0xe4, 0x00);
	HAL_PAI2C_Write(0xeb, 0x00);
	HAL_PAI2C_Write(0xec, 0x00);
	HAL_PAI2C_Write(0xed, 0x00);
	HAL_PAI2C_Write(0xee, 0x00);
	HAL_PAI2C_Write(0x00, 0x01);
	HAL_PAI2C_Write(0xEF, 0x01);
}

void HAL_SetSensorPreview(void) USING_0
{
	HAL_PAI2C_Write(0xEF, 0x01);//bank1
	HAL_PAI2C_Write(0x04, 0xfc);
	HAL_PAI2C_Write(0x05, 0x01);
	HAL_PAI2C_Write(0x0e, 0x00);
	HAL_PAI2C_Write(0x0f, 0x00);
	HAL_PAI2C_Write(0x06, 0x0f);//gb gain
	HAL_PAI2C_Write(0x07, 0x0f);//b gain
	HAL_PAI2C_Write(0x08, 0x0f);//gr gain
	HAL_PAI2C_Write(0x09, 0x0f);//r gain
	HAL_PAI2C_Write(0x10, 0x0f);//gain
	HAL_PAI2C_Write(0x42, 0x0c);//0x0fx2gain
	HAL_PAI2C_Write(0x58, 0x00);//0x18x2gain
	HAL_PAI2C_Write(0x11, 0x01);	//update
}
/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontGainSet                                                     *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Sensor gain                                                           *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust gain value for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, gain value to set                                                    *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT8 HAL_FrontGainSet_Intr(
    UINT32 value,
    UINT32  option
)USING_1
{
#if 1
	UINT32 tmp0;
	UINT8 regaddr[4], regdata[4];
	UINT8 gainvalue;

	tmp0 = option & 0x01;                 /* Sync VD ? */

	gainvalue= gaintbl[value];

	regaddr[0] = 0x10;
	regdata[0] = gainvalue & 0x1f;
	regaddr[1] = 0x42;
	regdata[1] = (( gainvalue & 0x60)>>5) |0x0c;
	regaddr[2] = 0x58;
	regdata[2] = (( gainvalue & 0x80)>>3) |0x00;
	regaddr[3] = 0x11;
	regdata[3] = 0x01;
	HAL_WriteSSC(regaddr, regdata, 0x04, tmp0 | 0x02);
#endif
	return SUCCESS;
}

UINT8 HAL_FrontGainSet(
    UINT32 value,
    UINT32  option
)
{
#if 1
	UINT32 tmp0;
	UINT8 regaddr[4], regdata[4];
	UINT8 gainvalue;

	tmp0 = option & 0x01;                 /* Sync VD ? */

	gainvalue= gaintbl[value];

	regaddr[0] = 0x10;
	regdata[0] = gainvalue & 0x1f;
	regaddr[1] = 0x42;
	regdata[1] = (( gainvalue & 0x60)>>5) |0x0c;
	regaddr[2] = 0x58;
	regdata[2] = (( gainvalue & 0x80)>>3) |0x00;
	regaddr[3] = 0x11;
	regdata[3] = 0x01;
	HAL_WriteSSC(regaddr, regdata, 0x04, tmp0 | 0x02);
#endif
	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontExpTimeSet                                                  *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control exposure time                                                         *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust exposure time value                                                       *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, expossure time value to set, unit 0.1ms                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT32
HAL_FrontExpTimeSet_Intr(
	UINT32 value,
	UINT32 option
) USING_1
{
#if 1
	UINT16 ltotal, ftotal, espline,espline_ny;
	UINT32 pixclk;
	UINT8  regaddr[3], regdata[3];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */
	pixclk = PREV_PCLKFREQ;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;

	espline_ny =509-espline;
	if(espline_ny<=0)
	{
		espline_ny=0;
	}

	regaddr[0] = 0x0e;
	regdata[0] = espline_ny &0xff;
	regaddr[1] = 0x0f;
	regdata[1] = (espline_ny>>8);
	regaddr[2] = 0x11;
	regdata[2] = 0x01;
	HAL_WriteSSC(regaddr, regdata, 0x03, option | 0x02);
#endif
	return SUCCESS;
}


UINT32
HAL_FrontExpTimeSet(
	UINT32 value,
	UINT32 option
)
{
#if 1
	UINT16 ltotal, ftotal, espline,espline_ny;
	UINT32 pixclk;
	UINT8  regaddr[3], regdata[3];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */
	pixclk = PREV_PCLKFREQ;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;

	espline_ny =509-espline;
	if(espline_ny<=0)
	{
		espline_ny=0;
	}

	regaddr[0] = 0x0e;
	regdata[0] = espline_ny &0xff;
	regaddr[1] = 0x0f;
	regdata[1] = (espline_ny>>8);
	regaddr[2] = 0x11;
	regdata[2] = 0x01;
	HAL_WriteSSC(regaddr, regdata, 0x03, option | 0x02);
#endif
	return SUCCESS;
}


/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontFrameRateSet                                                *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Frame Rate                                                            *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust Frame rate for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, frame rate value to set                                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT32
HAL_FrontFrameRateSet(
    UINT32 value,
    UINT32 option
)
{
	#if 0
	value = value;
	option = option;

	UINT32 ltotal, ftotal;
	UINT32 pixclk, tmp0;

	UINT8 regaddr[4], regdata[4];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */

	pixclk = PREV_PCLKFREQ;
	option = option & 0x01;

	tmp0=(pixclk * 10) /(ltotal * value);

	if (tmp0 >= ftotal ) {
		tmp0 =tmp0-ftotal;
	}
	else {
	    tmp0 =0;
	};
	regaddr[0] = 0x46;//0x2D;
	regdata[0] = tmp0;
	regaddr[1] = 0x47;//0x2E;
	regdata[1] = tmp0 >> 8;
	HAL_WriteSSC(regaddr, regdata, 0x02, option | 0x02);
#endif
	return 0;
}

void HAL_ExpTransToSnap(void)
{
#if 0
	UINT32 sespline;
	UINT8  regaddr[8], regdata[8];
	UINT8  sgain,factor;

	sgain=SNAP_GAIN;
	if (sgain>=0x50)
	{
		if(sgain>=0xf0)//div 8
		{
			sgain=sgain & 0x1f;
			regaddr[0] = 0x10;
			regdata[0] = sgain & 0x1f;
			factor=8;
		}
		else if (sgain>=0x70)//div 4
		{
			sgain=sgain & 0x1f;
			regaddr[0] = 0x10;
			regdata[0] = sgain & 0x1f;
			factor=4;
		}
		else if(sgain>=0x50)//div 2
		{
			sgain=sgain & 0x1f;
			regaddr[0] = 0x10;
			regdata[0] = sgain & 0x1f;
			factor=2;
		}
		regaddr[1] =0x42;
		regdata[1] =0x0c;
		regaddr[2] =0x58;
		regdata[2] =0x00;

		sespline =( PREV_PCLKFREQ*10)/ (Sensor_Snap_LineTotal * SNAP_SHUTTER);
		sespline=sespline*factor;

		if (sespline>=Sensor_Snap_FrameTotal)
		{
			regaddr[3] = 0x04;
			regdata[3] = sespline &0xff;
			regaddr[4] = 0x05;
			regdata[4] = (sespline>>8);
			regaddr[5] = 0x0e;
			regdata[5] = 0x00;
			regaddr[6] = 0x0f;
			regdata[6] = 0x00;
		}
		else
		{
			sespline=509-sespline;
			regaddr[3] = 0x04;
			regdata[3] = 0xfc;
			regaddr[4] = 0x05;
			regdata[4] = 0x01;
			regaddr[5] = 0x0e;
			regdata[5] =sespline &0xff;
			regaddr[6] = 0x0f;
			regdata[6] =(sespline>>8);
		}
		regaddr[7] = 0x11;
		regdata[7] = 0x01;
		HAL_WriteSSC(regaddr, regdata, 0x08, 0x03);
	}
//#else
	UINT8 kf_gain;
	UINT32 kf_time;

	kf_gain=LVtbl50[40].gain;
	kf_time=LVtbl50[40].shutter;

	printf("%bu,%ld\n",kf_gain,kf_time);

	HAL_FrontGainSet(kf_gain,1);
	HAL_FrontExpTimeSet(kf_time,1);

#endif


}


UINT8 HAL_SnapRaw8(UINT8 mode, UINT8 opt) USING_0
{
	mode = mode;

	if( opt == 1 ) {
		HAL_FrontVdWait(1,1);
		HAL_FrontVdWait(1,1);
		HAL_ExpTransToSnap();
	}

	HAL_FrontVdWait(0,1); // 20081024 mantis 32544 wenguo
	HAL_CdspWriteSkip( 6, 1 );
	HAL_FrontVdWait(0,1);
	HAL_FrontVdWait(1,1);
	HAL_FrontInputGate(0x11);
	HAL_CDSPeofWait(1);
	return 0;
}

void HalSensorPrePV(
	void
)
{
	HAL_FrontVdWait(0,1);
	HAL_FrontVdWait(0,1);
	return;
}

#if 1 /*cs added for gain*/
UINT8 HAL_MaxGainIndexGet()
{
	return(MAX_GAIN_IDX);
}
UINT8 HAL_MinGainIndexGet()
{
	return(MIN_GAIN_IDX);
}

UINT8 HAL_MaxExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MAX_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MAX_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MAX_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MAX_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	}

}

UINT8 HAL_MinExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MIN_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MIN_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MIN_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MIN_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	}
}

void HAL_ColorSatReduce_Intr(UINT8 idx)	   // reduce color saturation and edge
{
#if 0
	if(idx > K_GAIN_Large) {
		if( sensorDriverGainClass != 3 ) {
			HAL_CdspColorMSet_Intr(cmt[2][0],cmt[2][1],cmt[2][2],cmt[2][3],cmt[2][4],cmt[2][5],cmt[2][6],cmt[2][7],cmt[2][8]);
			XBYTE[0x21D3] = 0x08;
			XBYTE[0x21D0] = 0x08;

			sensorDriverGainClass = 3;
		}
	}
	else if (idx > K_GAIN_Middle) {
		if( sensorDriverGainClass != 2 ) {
			HAL_CdspColorMSet_Intr(cmt[1][0],cmt[1][1],cmt[1][2],cmt[1][3],cmt[1][4],cmt[1][5],cmt[1][6],cmt[1][7],cmt[1][8]);
			XBYTE[0x21D3] = 0x06;
			XBYTE[0x21D0] = 0x08;

			sensorDriverGainClass = 2;
		}
	}
	else if (idx > K_GAIN_Small) {
		if( sensorDriverGainClass != 1 ) {
			HAL_CdspColorMSet_Intr(cmt[0][0],cmt[0][1],cmt[0][2],cmt[0][3],cmt[0][4],cmt[0][5],cmt[0][6],cmt[0][7],cmt[0][8]); // for hgnoise
			XBYTE[0x21D3] = 0x04;
			XBYTE[0x21D0] = 0x0A;

			sensorDriverGainClass = 1;
		}
	}
	else {
		if( sensorDriverGainClass != 0 ) {
			cdspColorMatrixRestore_Intr();
			XBYTE[0x21D3] = 0x02;
			XBYTE[0x21D0] = 0x02;

			sensorDriverGainClass = 0;
		}
	};
#endif
}
#endif
#endif
